package com.example.tse.rbyair.PKmy.fragment;

import android.annotation.SuppressLint;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.LinearLayout;
import android.widget.ListView;

import com.example.tse.rbhttpservice.service.HttpService;
import com.example.tse.rbhttpservice.service.RemoteServiceFactory;
import com.example.tse.rbhttpservice.service.RemoteServiceListenerT;
import com.example.tse.rbhttpservice.service.member.model.MemberCouponGetList;
import com.example.tse.rbhttpservice.service.member.model.MemberCouponGetListRequest;
import com.example.tse.rbhttpservice.service.member.model.MemberCouponGetListResponse;
import com.example.tse.rbyair.R;
import com.example.tse.rbyair.util.AppInforUtil;

import org.kymjs.kjframe.adapter.CommonAdapter;
import org.kymjs.kjframe.adapter.ViewHolder;
import org.kymjs.kjframe.log.L;
import org.kymjs.kjframe.widget.KJListView;

import java.util.ArrayList;
import java.util.List;

@SuppressLint("NewApi")
public class UnusedCouponFragment extends Fragment{
	String status = "0";
	int a;
	View messageLayout;
	ListView listView;
	LinearLayout emptyLayout;
	private int page = 1;
	private int size = 10;
	private KJListView pulltorefershList;
	private CommonAdapter adapter;
	List<MemberCouponGetList> couponLists;

	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container,
			Bundle savedInstanceState) {
		messageLayout = LayoutInflater.from(getActivity()).inflate(
				R.layout.coupon_list, container, false);
		initView();
		initListView();

		return messageLayout;
	}

	private void initView() {
		pulltorefershList = (KJListView)messageLayout.findViewById(R.id.pulltorefersh_list);

		couponLists = new ArrayList<>();

		adapter = new CommonAdapter<MemberCouponGetList>(getActivity(),couponLists,R.layout.coupon_list_item){

			@Override
			public int getViewTypeCount() {
				return super.getViewTypeCount();
			}

			@Override
			public int getItemViewType(int position) {
				return super.getItemViewType(position);
			}

			@Override
			public void convert(ViewHolder holder, MemberCouponGetList memberCouponGetList, int position) {
				holder.setText(R.id.coupon_price, "￥"+ AppInforUtil.formatPrice3(couponLists.get(position).getPrice()));
			}
		};
		pulltorefershList.setAdapter(adapter);
		getCouponList();
	}

	private void getCouponList(){
		/**
		 *   需要登入
		 *   优惠券状态，0未使用、1已使用、2已过期，默认0
		 */
		final MemberCouponGetListRequest couponList = new MemberCouponGetListRequest() {
			@Override
			public String giveStatus() {
				return "0";
			}
		};


		RemoteServiceFactory.getInstance().getMemberCouponService(getActivity()).getList(couponList, new RemoteServiceListenerT<MemberCouponGetListResponse>() {

			@Override
			public void PreStart() {
				super.PreStart();
				L.d("lichun","UnusedCouponFragment HttpService.token:"+HttpService.token);
			}

			@Override
			public void okData(MemberCouponGetListResponse memberCouponGetListResponse) {
				super.okData(memberCouponGetListResponse);

				List<MemberCouponGetList> couponList = memberCouponGetListResponse.getGetList();
				ArrayList<MemberCouponGetList> tmp = (ArrayList<MemberCouponGetList>) memberCouponGetListResponse.getGetList();

				L.d("WelcomeActivity  couponList.size():" + couponList.size());
				couponLists.clear();
				couponLists.addAll(tmp);
				adapter.notifyDataSetChanged();
				for (int i = 0; i < couponList.size(); i++) {
					MemberCouponGetList cp = couponList.get(i);

					L.d("WelcomeActivity  Cp.getName():" + cp.getName() + " getCouponId:" + cp.getCouponId());

					List<String> rulesList = cp.getRule();
					L.d("WelcomeActivity rulesList.size():" + rulesList.size());
					for (int j = 0; j < rulesList.size(); j++) {
						String rule = rulesList.get(j);
						L.d("WelcomeActivity rule:" + rule);
					}
				}
			}
		});
	}

	@Override
	public void setUserVisibleHint(boolean isVisibleToUser) {
		super.setUserVisibleHint(isVisibleToUser);
	}

	public void initListView() {

	}

	protected void initEmptyLayout() {
		pulltorefershList.setVisibility(View.GONE);
		emptyLayout.setVisibility(View.VISIBLE);
	}
}
